|
|
|
|
|
|
|
For example, FormatCurrency formats a number in currency (money) style. You can test the FormatCurrency function by opening Visual Basic's Immediate window (a project does not have to be running). Type the following line into the Immediate window, and press Enter: |
|
|
|
|
|
|
|
|
Print FormatCurrency(1234) |
|
|
|
|
|
|
|
|
The output looks something like this: |
|
|
|
|
|
|
|
|
Notice the dollar ($) signs, comma separators (,), and the appropriate number of decimal places associated with money. You can control these settings in the Currency tab of the Regional Settings screen in the Control Panel. |
|
|
|
|
|
|
|
|
All the functions listed in Table 9.4, except for FormatDateTime, have the following optional parameters: |
|
|
|
|
|
|
|
|
NumDigitsAfterDecimalDetermines the number of decimal places in the output |
|
|
|
|
|
|
|
|
IncludeLeadingDigitDetermines whether a zero is displayed for fractional values (for example, 0.567 or .567) |
|
|
|
|
|
|
|
|
UseParensForNegativeNumbersDetermines whether negative numbers are returned with parentheses around them instead of the negative sign |
|
|
|
|
|
|
|
|
GroupDigitsDetermines whether a grouping symbol (that is, the comma) is used in the return value (for example, 1,234 or 1234) |
|
|
|
|
|
|
|
|
Many of these optional parameters, such as the IncludeLeadingDigit parameter, are known as Tristate parameters. A Tristate parameter has three settings, or states: vbTrue, vbFalse, and vbUseDefault. Well, really there are only two valid values: True or False. When you choose a setting of vbUseDefault, the function determines the True or False setting based on the Regional Settings in the Control Panel. |
|
|
|
|
|
|
|
|
New Date Format Functions |
|
|
|
|
|
|
|
|
The FormatDateTime function does not have any of the optional parameters listed in the preceding section. It simply accepts an expression representing a date or time, and a constant that represents one of the named date formats. The named date formats are listed later in the chapter in Table 9.7. Visual Basic displays a list of the constants as you type the function in the Code window. For example, type the following statement in the Immediate window: |
|
|
|
|
|
|
|
|
Print FormatDateTime("20:10",vbLongTime) |
|
|
|
|
|
|
|
|
The FormatDateTime function converts the military time value to the AM/PM time, 8:10. Notice that a string value is used for the first parameter. The expressions used by FormatDateTime must be able to be converted into a valid date or time; otherwise, an error occurs. |
|
|
|
|
|
|
|
|
Two other functions added to Visual Basic version 6 allow you to retrieve the printed name of the month or weekday from any date expression. These functions are MonthName and WeekDayName, respectively. They both accept a numeric value and return a string. Optional parameters allow you to specify whether to abbreviate the output: |
|
|
|
|
|